<?php
class db {
	/* 数据库链接 */
	public $con;
	/* 构造方法连接数据库 */
	public function __construct($conf) {
		$con = mysql_connect ( $conf ['DBhost'], $conf ['DBname'], $conf ['DBpasswd'] ) or die ( '数据库链接不成功！' );
		mysql_select_db ( $conf ['DBbase'] );
		mysql_set_charset ( 'utf8' );
		$this->con = $con;
	}
	
	/* count */
	public function count($sql) {
		$query = mysql_query ( $sql, $this->con );
		$result = mysql_fetch_array ( $query );
		return $result [0];
	}
	
	/* 数据库添加方法 */
	public function add($sql) {
		mysql_query ( $sql, $this->con );
		$rc = mysql_affected_rows ( $this->con );
		if ($rc > 0) {
			return mysql_insert_id ( $this->con );
		} else {
			return 0;
		}
	}
	/* mysql 查询 */
	public function select($sql) {
		$result = mysql_query ( $sql, $this->con );
		while ( $row = mysql_fetch_assoc ( $result ) ) {
			$arr [] = $row;
		}
		return $arr;
	}
	
	/* mysql 查询 */
	public function U_D($sql) {
		$result = mysql_query ( $sql, $this->con );
		return mysql_affected_rows ( $this->con );
	}
	
	/* 获取mysql 错误 */
	public function error() {
		return mysql_error ( $this->con );
	}
	
	/* 释放连接 */
	/* public function __destruct() {
		mysql_close ( $this->con );
	} */
}